home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 889 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.0 KB  |  47 lines

  1. Path: unix.sri.com!usenet
  2. From: mklenk@updike.sri.com (Mark Klenk)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help on Dynamically Allocating an Array?
  5. Date: 9 Jan 1996 22:53:52 GMT
  6. Organization: SRI International
  7. Message-ID: <4curm0$t65@unix.sri.com>
  8. References: <4cq273$aeq@mercury.IntNet.net>
  9. Reply-To: mklenk@updike.sri.com
  10. NNTP-Posting-Host: 204.75.161.40
  11.  
  12.  
  13. >I'm confused on who to dynamically allocate an array of any type. Any 
  14. >help would be much appreciated.
  15.  
  16. #include <stdlib.h>
  17.  
  18. #define NEW(type, num) \
  19.     ((type *)malloc(sizeof(type) * (num)))
  20.  
  21. typedef struct {
  22.     double real, imag;
  23. } Complex;
  24.  
  25. Complex * ComplexVectorAdd(Complex const * one, Complex const * two,
  26.                            int vector_size)
  27. {
  28.     Complex * result = NEW(Complex, vector_size);
  29.  
  30.     if (NULL != result) {
  31.         int i;
  32.         for (i = 0; i < vector_size; ++i) {
  33.             result[i].real = one[i].real + two[i].real;
  34.             result[i].imag = one[i].imag + two[i].imag;
  35.         }
  36.     }
  37.  
  38.     return result;
  39. }
  40.  
  41. ---
  42.  
  43. mklenk@coronacorp.com       (Mark Klenk)
  44.  
  45.  
  46.  
  47.